package com.sunye.rabbitBattle;


import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RabbitMqBaattleListen {


    //发送短信给用户手机
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "asynchronous.change",type = "fanout")
            )
    })
    public void asynchronousPhone(Map map){
        String phone = map.get("phone").toString();
        System.out.println("手机号"+phone+"发送验证码");
    }

    //发送邮箱验证码成功
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "asynchronous.change",type = "fanout")
            )
    })
    public void asynchronousEmail(Map map){
        String email = map.get("email").toString();
        System.out.println("邮箱"+email+"发送验证码");
    }




    //库存系统订阅下单消息
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "decoupling.change",type = "fanout")
            )
    })
    public void decouplingOrder(Map map){
        String goodId = map.get("goodId").toString();
        String userId = map.get("userId").toString();
        System.out.println("用户“"+userId+"”扣减商品“"+goodId+"”库存成功");
    }

    //下单操作
    @RabbitListener(queues = "seckillQueue")
    public void receive(Map map){
        String userId = map.get("userId").toString();
        String goodsId = map.get("goodsId").toString();
        System.out.println("接收消息======用户："+userId);
        System.out.println("接收消息======商品："+goodsId);
        //TODO 使用商品id去数据库查询库存
        //TODO 判断是否重复抢购

        //TODO 去数据插入下单数据
        System.out.println("用户下单成功");
    }




}
